# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=creduce
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=2.10.0.r96.g8d56bee
pkgrel=3
_commit=8d56bee3e1d2577fc8afd2ecc03b1323d6873404
pkgdesc="A C program reducer (mingw-w64)"
depends=('perl-Benchmark-Timer'
         'perl-Exporter-Lite'
         'perl-File-Which'
         'perl-Getopt-Tabular'
         'perl-Regexp-Common'
         "${MINGW_PACKAGE_PREFIX}-astyle"
         "${MINGW_PACKAGE_PREFIX}-indent"
         "${MINGW_PACKAGE_PREFIX}-clang")
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://github.com/csmith-project/creduce'
license=('spdx:NCSA')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "git")
source=(${_realname}::git+https://github.com/csmith-project/creduce#commit=${_commit}
        "llvm-config-cygpath"
        "001-port-to-llvm-16.patch")
sha256sums=('SKIP'
            '03a7239d1e26d8a5dd5f405e10826679725cae6c01a9c37e80c6f1c184dad0a9'
            'c7de1b06bd9fd840db1c5e9d3f9b9516a76cd14d9e969547bdaa995c5af4bbab')

pkgver() {
  cd "${srcdir}/${_realname}"
  git describe --long ${_commit} | sed 's/creduce-//g;s/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

prepare() {
  cd "${srcdir}"/${_realname}
  # https://github.com/csmith-project/creduce/pull/254
  git apply "${srcdir}"/001-port-to-llvm-16.patch

  [[ -d "${srcdir}"/tools ]] && rm -rf "${srcdir}"tools
  mkdir -p "${srcdir}"/tools
  cp -f "${srcdir}"/llvm-config-cygpath "${srcdir}"/tools/llvm-config
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  PATH="${srcdir}/tools:$PATH" \
  ../${_realname}/configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST}

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install

  install -Dm644 "../${_realname}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"

  # Point creduce perl script:
  # #!/${MINGW_PREFIX} -> #!/usr
  # ..  to msys2 perl.
  sed "s|#!${MINGW_PREFIX}|#!/usr|g" \
    -i ${pkgdir}${MINGW_PREFIX}/bin/creduce
}
